package com.liyue.animationutils;

import android.view.animation.Interpolator;

/**
 * @author liyue
 * @date 2021/6
 *
 * 动画的弹性因数
 *
 * 用法示例：
 * private void onScaleAnimation(View v) {
 *         ObjectAnimator animatorX = ObjectAnimator.ofFloat(v, "scaleX", 1.0f, 0.3f);
 *         ObjectAnimator animatorY = ObjectAnimator.ofFloat(v, "scaleY", 1.0f, 0.3f);
 *         AnimatorSet set = new AnimatorSet();
 *         set.play(animatorX).with(animatorY);
 *         set.setDuration(2000);
 *         set.setInterpolator(new SpringScaleInterpolator(0.8f));
 *         set.start();
 *         set.addListener(new Animator.AnimatorListener() {
 *             @Override
 *             public void onAnimationStart(Animator animation) {
 *             }
 *
 *             @Override
 *             public void onAnimationEnd(Animator animation) {
 *             }
 *             @Override
 *             public void onAnimationCancel(Animator animation) {
 *             }
 *
 *             @Override
 *             public void onAnimationRepeat(Animator animation) {
 *             }
 *         });
 *     }
 **/
public class SpringScaleInterpolator implements Interpolator {
    //弹性因数
    private float factor;

    public SpringScaleInterpolator(float factor) {
        this.factor = factor;
    }

    @Override
    public float getInterpolation(float input) {

        return (float) (Math.pow(2, -10 * input) * Math.sin((input - factor / 4) * (2 * Math.PI) / factor) + 1);
    }


}

